package android.slc.appbase.api.slc.config;

import android.slc.box.db.sp.DbCd;
import io.reactivex.rxjava3.annotations.NonNull;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by on the way on 2018/9/18.
 */
public class ApiSp {
    //手动
    public final static int CONFIG_MODE_HM = 0;
    //自动
    public final static int CONFIG_MODE_AUTO = 1;
    private final static String SP_NAME = "api_config";
    private final static String KEY_APPLY_CACHE_API = "applyCacheApi";
    private final static String KEY_SERVICE_MODE = "serviceMode";
    private final static String KEY_SERVICE_API_ADDRESS = "serviceApiAddress";
    private final static String KEY_SOCKET_MODE = "socketMode";
    private final static String KEY_SOCKET_ADDRESS = "socketAddress";
    private final static String KEY_SOCKET_PORT = "socketPort";

    private final DbCd db;

    protected ApiSp() {
        db = new DbCd("api_config");
    }

    private static class Holder {
        private static final ApiSp INSTANCE = new ApiSp();
    }

    public static ApiSp getInstance() {
        return ApiSp.Holder.INSTANCE;
    }

    public String getStringTrim(@NonNull final String key, final String defaultValue) {
        String value = db.getString(key, defaultValue);
        return StringUtils.trim(value);
    }

    //apply
    public boolean isApplyCacheApi() {
        try {
            return db.getBoolean(KEY_APPLY_CACHE_API, false);
        } catch (Exception e) {
            return false;
        }
    }

    public void setApplyCacheApi(boolean applyCacheApi) {
        db.putValue(KEY_APPLY_CACHE_API, applyCacheApi);
    }

    //service
    public int getServiceMode() {
        return getServiceMode(CONFIG_MODE_HM);
    }

    public int getServiceMode(int defValue) {
        return db.getInt(KEY_SERVICE_MODE, defValue);
    }

    public void setServiceMode(int value) {
        db.putValue(KEY_SERVICE_MODE, value);
    }

    public String getServiceApiAddress() {
        return getServiceApiAddress(null);
    }

    public String getServiceApiAddress(String defValue) {
        return getStringTrim(KEY_SERVICE_API_ADDRESS, defValue);
    }

    public void setServiceApiAddress(String value) {
        setServiceApiAddress(value, true);
    }

    public void setServiceApiAddress(String value, boolean forceSave) {
        if (forceSave || StringUtils.isBlank(getServiceApiAddress())) {
            db.putValue(KEY_SERVICE_API_ADDRESS, value);
        }
    }

    //socket
    public int getSocketMode() {
        return getSocketMode(CONFIG_MODE_HM);
    }

    public int getSocketMode(int defValue) {
        return db.getInt(KEY_SOCKET_MODE, defValue);
    }

    public void setSocketMode(int value) {
        db.putValue(KEY_SOCKET_MODE, value);
    }

    public String getSocketAddress() {
        return getSocketAddress(null);
    }

    public String getSocketAddress(String defValue) {
        return getStringTrim(KEY_SOCKET_ADDRESS, defValue);
    }

    public void setSocketAddress(String value) {
        setSocketAddress(value, true);
    }

    public void setSocketAddress(String value, boolean forceSave) {
        if (forceSave || StringUtils.isBlank(getSocketAddress())) {
            db.putValue(KEY_SOCKET_ADDRESS, value);
        }
    }

    public int getSocketPort() {
        return getSocketPort(-1);
    }

    public int getSocketPort(int defValue) {
        return db.getInt(KEY_SOCKET_PORT, defValue);
    }

    public void setSocketPort(int value) {
        setSocketPort(value, true);
    }

    public void setSocketPort(int value, boolean forceSave) {
        if (forceSave || getSocketPort() == -1) {
            db.putValue(KEY_SOCKET_PORT, value);
        }
    }

}
